package com.dq.Controller;

import com.dq.Pojo.Grade;
import com.dq.Pojo.Percentage;
import com.dq.Pojo.Result;
import com.dq.Service.GradeService;
import com.dq.Service.PercentageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    private GradeService gradeService;
    @Autowired
    private PercentageService percentageService;

    @PostMapping("/addORupdate")
    public Result addORupdate(@RequestBody Grade grade){
        Percentage percentage=percentageService.getByid(grade.getClassid(),grade.getCourseid());
        if(percentage==null){
            return Result.error("请先给课程配置分数比!!!");
        }else{
            //计算平时分
            float usual= grade.getAttendNum()*percentage.getAttend()
                    +grade.getClasstaskNum()* percentage.getClasstask()
                    +grade.getAftertaskNum()* percentage.getAftertask()
                    +grade.getExperimentNum()* percentage.getExperiment();
            //计算课程总分
            float total= usual * percentage.getUsualscore()+grade.getFinalscoreNum()* percentage.getFinalscore();
            grade.setUsualscoreNum(usual);
            grade.setTotalNum(total);
            if (grade.getGradeid()==null){
                Integer id=grade.getStuid();
                Grade g=gradeService.findBystuid(id);
                if(g==null){
                    gradeService.add(grade);
                }else {
                    return Result.error("此学生成绩已录入!!!");
                }
            }else {
                    gradeService.update(grade);
            }
            return Result.success();
        }
    }

    @GetMapping("/findAll")
    public Result findAll(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String stuid,
                          @RequestParam Integer courseid,
                          @RequestParam Integer classid
    ){
        PageInfo<Grade> pageInfo=gradeService.findAll(pageNum,pageSize,stuid,courseid,classid);
        return  Result.success(pageInfo);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id){
        gradeService.delete(id);
        return Result.success();
    }
    //学生查看成绩
    @GetMapping("/stuFind")
    public Result stu(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String stuid,
                      @RequestParam String courseid){
        PageInfo<Grade> gradePageInfo=gradeService.findstu(pageNum,pageSize,stuid,courseid);
        return Result.success(gradePageInfo);
    }


}
